package leetcode.editor.cn;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: yangwk
 * @date: 2022/12/28
 * @description:
 **/
public class P454 {
    public static void main(String[] args) {
        int[] nums1 = {1, 2}, nums2 = {-2, -1}, nums3 = {-1, 2}, nums4 = {0, 2};
        System.out.println(fourSumCount(nums1, nums2, nums3, nums4));
    }

    public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map1 = new HashMap<>();
        Map<Integer, Integer> map2 = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            int e1 = nums1[i];
            for (int j = 0; j < nums2.length; j++) {
                int e2 = nums2[j];
                map1.compute(e1 + e2, (integer, integer2) -> integer2 == null ? 1 : integer2 + 1);
            }
        }
        for (int i = 0; i < nums3.length; i++) {
            int e1 = nums3[i];
            for (int j = 0; j < nums4.length; j++) {
                int e2 = nums4[j];
                map2.compute(e1 + e2, (integer, integer2) -> integer2 == null ? 1 : integer2 + 1);
            }
        }
        int count = 0;
        for (Map.Entry<Integer, Integer> entry : map1.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            Integer integer = map2.getOrDefault(-key, 0);
            count += value * integer;
        }
        return count;
    }
}
